package com.hl233.framework.controller;

import com.hl233.framework.common.Constants;
import com.hl233.framework.entity.BackEntity;
import com.hl233.framework.utils.DateUtil;
import com.hl233.framework.utils.KafkaUtil;
import com.hl233.framework.utils.RandomCode;
import com.hl233.framework.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Random;

/**
 * Kafka生产消息测试
 *
 * @author 张振强15065206763
 * @create 2024-09-03
 */
@RestController
@RequestMapping("/kafka")
public class KafkaController {

    @Autowired
    private KafkaUtil kafkaUtil;

    @ApiOperation(value = "生产消息")
    @GetMapping("/send/{val}")
    public Result add(@PathVariable("val") String val) {
        for (int i = 0; i < 100000; i++) {
            try {
                String res = RandomCode.code(6);
                res += "<>" + DateUtil.dateToString(new Date(), DateUtil.FMT_Y_M_D_H_M_S);
                kafkaUtil.send(Constants.HL233_DEVICE_MSG_TOPIC, res);
                System.out.println("生产的数据" + res);
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return Result.ok();
    }


}
